package doublelistdemo;

    /**
     * @Author 12629
     * @Date 2022/3/15 21:25
     * @Description：
     */
    class ListNode {
        public int val;//值
        public ListNode next;//后继信息
        public ListNode prev;//前驱信息

        public ListNode(int val) {
            this.val = val;
        }
    }

    public class MyLinkedList{

        public ListNode head;//标记双向链表的头节点

        public ListNode last;//标记双向链表的尾巴

        //头插法
        public void addFirst(int data){
            ListNode node = new ListNode(data);
            if(head == null){
                head = node;
                last = node;
            }
            node.next = head;
            head.prev = node;
            head = node;
        }

        //尾插法
        public void addLast(int data){
            ListNode node = new ListNode(data);
            if(head == null){
                head = node;
                last = node;
            }
            node.next = last;
            last.prev = node;
            last = node;
        }

        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            if (index < 0 || index >size()){
                throw new RuntimeException("");
            }
            ListNode node = new ListNode(data);

        }
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            ListNode cur = this.head;
            while (cur != null) {
                if(cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
        //删除第一次出现关键字为key的节点
        public void remove(int key){}
        //删除所有值为key的节点
        public void removeAllKey(int key){}
        //得到单链表的长度
        public int size(){
            int count = 0;
            ListNode cur = this.head;
            while (cur != null) {
                count++;
                cur = cur.next;
            }
            return count;
        }
        public void display() {
            ListNode cur = this.head;
            while (cur != null) {
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
        }
        public void clear(){

        }
    }

